package com.mobile.check.present;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import io.reactivex.Observable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import com.mobile.check.beans.TaskInfo;
import com.mobile.check.db.EntityManager;
import com.mobile.check.fragments.JianceFragment;

/**
 * 类名: JiancePresent
 * 作者: 李学伟
 * 日期: 2020年10月13日 11:31
 * 描述:
 */
public class JiancePresent extends XPresent<JianceFragment> {
    public void queryTaskInfo(int taskType) {
        final List<TaskInfo> dataList = new ArrayList<>();
        List<TaskInfo> dbList = EntityManager.getInstance().queryTaskInfo(taskType);
        Observable<TaskInfo> dbObservable = Observable.fromIterable(dbList);
//        Observable<TaskInfo> localObservable = loadDatasFromFile(taskType);
        dbObservable.subscribe(new Consumer<TaskInfo>() {
            @Override
            public void accept(TaskInfo taskInfo) throws Exception {
                dataList.add(taskInfo);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        }, new Action() {
            @Override
            public void run() throws Exception {
                getV().onSuccess(true, dataList);
            }
        });

    }

//    private Observable loadDatasFromFile(final int taskType) {
//        String type;
//        switch (taskType) {
//            case 0:
//                type = "检测任务";
//                break;
//            case 1:
//                type = "复检任务";
//                break;
//            case 2:
//                type = "跟踪任务";
//                break;
//            default:
//                type = "检测任务";
//        }
//        File sd = Environment.getExternalStorageDirectory();
//        File f = new File(sd.getPath().concat("/").concat("检测系统").concat("/").concat(type).concat("/")); // 获取文件句柄
//        if (!f.exists()) {
//            f.mkdirs();
//        }
//        File[] files = f.listFiles();
//        return Observable.fromArray(files).filter(new Predicate<File>() {
//            @Override
//            public boolean test(File file) throws Exception {
//                String fileName = file.getName();
//                return fileName.endsWith(".xlsx");
//            }
//        }).map(new Function<File, TaskInfo>() {
//            @Override
//            public TaskInfo apply(File file) throws Exception {
//                TaskInfo taskInfo = new TaskInfo();
//                taskInfo.setTask_name(file.getName());
//                taskInfo.setTaskFilePath(file.getPath());
//                taskInfo.setLocalFile(true);
//                taskInfo.setTaskType(taskType);
//                return taskInfo;
//            }
//        });
//    }
}
